$(function () {
    $.ajax({
        url: "/api/menu/findSysMenuByUserId",
        dataType: "json",
        success: function (data) {
            menuInit(data.data);
        }
    });
});


function menuInit(menuJson) {
    var menu = null;
    var html = null;
    var chidLen = null;
    var child = null;
    if (menuJson != undefined && menuJson.length > 0) {

        for (var i = 0; i < menuJson.length; i++) {
            menu = menuJson[i];
            if(menu.children!=undefined){
                if (i == 0) {
                    html = $(' <li menu-id="' + i + '" class="active treeview "><li>');
                } else {
                    html = $(' <li menu-id="' + i + '" class="treeview "><li>');
                }

                $(".sidebar .sidebar-menu").append(html);
                if(menu.url == null){
                    html = $(' <a href="javascript:void(0)"> <i class="'+menu.cls+'"></i> <span>' + menu.name + '</span> <span class="pull-right-container"> <i class="fa fa-angle-left pull-right"></i> </span> </a> <ul menuUl-id="' + i + '" class="treeview-menu"> </ul>');
                }else {
                    html = $(' <a href="' + menu.url + '"> <i class="'+menu.cls+'"></i> <span>' + menu.name + '</span> <span class="pull-right-container"> <i class="fa fa-angle-left pull-right"></i> </span> </a> <ul menuUl-id="' + i + '" class="treeview-menu"> </ul>');
                }
            }else {
                if (i == 0) {
                    html = $(' <li menu-id="' + i + '" class="active"><li>');
                } else {
                    html = $(' <li menu-id="' + i + '" ><li>');
                }

                $(".sidebar .sidebar-menu").append(html);
                if(menu.url == null){
                    html = $(' <a href="javascript:void(0)"> <i class="'+menu.cls+'"></i> <span>' + menu.name + '</span> </a> <ul menuUl-id="' + i + '" class="treeview-menu"> </ul>');
                }else {
                    html = $(' <a href="' + menu.url + '"> <i class="'+menu.cls+'"></i> <span>' + menu.name + '</span> </a> <ul menuUl-id="' + i + '" class="treeview-menu"> </ul>');
                }
            }


            $('[menu-id="' + i + '"]').append(html);
            if(menu.children !=undefined){
                chidLen = menu.children.length;
                for (var j = 0; j <= chidLen; j++) {
                    if (j != chidLen) {
                        child = menu.children[j];
                        if (j == 0 && i == 0) {
                            html = $('<li class="active"><a href="' + child.url + '" target="'+child.target+'"><i class="'+child.cls+'"></i> ' + child.name + '</a></li>');
                        } else {
                            html = $('<li ><a href="' + child.url + '" target="'+child.target+'"><i class="'+child.cls+'"></i> ' + child.name + '</a></li>');
                        }
                        $('[menuUl-id="' + i + '"]').append(html);
                    }
                }
            }
        }
    }
}